const app = getApp();
import * as echarts from '../../ec-canvas/echarts';
function pieChart(chart,valueList) {
    var option = {
      tooltip: {
        trigger: 'item'
      },
      legend: {
       bottom: '1%',
        left: 'center'
      },
  
      series: [
        {
          type: 'pie',
          center: ['50%', '40%'],
          radius: ['30%', '70%'],
          avoidLabelOverlap: false,
          label: {
            show: false,
            position: 'center'
          },
          
          labelLine: {
            show: false
          },
          itemStyle: {
            normal: {
              label: {
                show: true,
                position: 'inner',
                formatter: function(params) {
                  return (params.percent - 0).toFixed(0) + '%'
                }
              },
              labelLine: {
                show: false
              }
            },
            emphasis: {
              label: {
                show: true,
                formatter: "{b}\n{d}%"
              }
            },
          },
          data: valueList
        }
      ]
    };
      chart.setOption(option);
    }
  
Page({

    /**
     * 页面的初始数据
     */
    data: {
        icons:'https://image.browin.net.cn/icons/',
        now:'today',
        ec: {
            lazyLoad: true
          },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let d = app.globalData;
        var open=wx.getStorageSync('open');
        if(open.user_id == 0 || open.user_id==undefined){
            console.log('open is null')
            wx.navigateTo({
              url: '../mine/bindPhone',
            })
        }
        this.setData({
            platform:d.platform,    
            open:open,
            width:d.windowWidth,
            height:d.windowHeight,
            statusBarHeight:d.statusBarHeight,
            navHeight:d.navHeight,
            navWidth:d.navWidth,
            navRight:d.navRight,
            marginTop:d.navTitleTop,
            domain:d.domain,
            uploadUrl:d.uploadUrl,
          })
    },

    goto:function(e){
        console.log(e);
        var url = e.currentTarget.dataset.url;
        var index = e.currentTarget.dataset.index;     
        app.goto(url,index)
     
        console.log(url);
    },
    goBack(){
        wx.navigateBack();
    },
    goIndex(){
        var  url ='/pages/mine/index';
        console.log(url);
        wx.reLaunch({
          url:url
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        var open=wx.getStorageSync('open');
        if(open.user_id == 0 || open.user_id==undefined){
            console.log('open is null')
            wx.navigateTo({
              url: '../mine/bindPhone',
            })
        }
        this.getIndex();

    },
    getIndex(){
        var open =  wx.getStorageSync('open');
        let that = this;
        var data = [];
        data['user_id']= open.user_id;
        var url = 'customer/getIndex';
        //data['user_id'] = 0;
      app.request(url,data,'get').then(res=>{ 
        console.log('customer getIndex'); 
         console.log(res);
     
         this.initChart(res.pie);
         wx.setStorageSync('depart',res.depart);
         that.setData({
             'role':res.role,
             'depart':res.depart,
             'store':res.store,
             'follow':res.follow,
             'create':res.create,
             'order':res.order,
             'sale':res.sale,
            
             
         });

       //  that.wxUpdate(); //that.wxLogIn();  
      })
    }, 
    initChart: function (valueList) {
        this.selectComponent('#mychart-dom-pie').init((canvas, width, height, dpr) => {
          const chart = echarts.init(canvas, null, {
            width: width,
            height: height,
            devicePixelRatio: dpr
          });
          pieChart(chart,valueList)
          this.chart = chart;
          canvas.setChart(chart);
          return chart;
        })
      },
    selectNow(e){
        var now = this.data.now;
        let that = this;
        console.log(now);
        var value = e.currentTarget.dataset.now;
        console.log(value);
        if(now!=value){
        var open =  wx.getStorageSync('open');
        var data = [];
        data['user_id']= open.user_id;
        data['now']= value;
        var url = 'customer/getNow';

      app.request(url,data,'get').then(res=>{ 
        console.log('customer getIndex'); 
         console.log(res);
     
         this.initChart(res.pie);
         that.setData({        
             'follow':res.follow,
             'create':res.create,
             'order':res.order,
             'sale':res.sale,       
             
         });

       //  that.wxUpdate(); //that.wxLogIn();  
      })
            that.setData({
                now:value
            })
        }

      },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})